#ifndef _RAW_SAVE_H_
#define _RAW_SAVE_H_

#include "config.hpp"
#include "muxer_mp4.hpp"
#include "ringbuffer.hpp"
#include <cstring>
#include <iostream>
#include <thread>

extern "C" {
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libavutil/avutil.h>
#include <libavutil/imgutils.h>
}

class raw_save {
private:
    std::string ip;
    bool running;
    std::thread handle;
    ringbuffer* rbuf;
    AVFormatContext* fmtctx;
    AVStream* stream;
    AVCodec* codec;
    std::string filename;

public:
    raw_save();
    raw_save(ringbuffer* ringbuffer, std::string ip);
    ~raw_save();

    int set(ringbuffer* ringbuffer, std::string ip);
    void thread();
    void start();
    void stop();
    int open(std::string filename);
    int write(AVPacket* packet);
    int close();
    void msleep(int ms);
};

#endif